//
//  NMDrrBlock.m
//  podrow
//
//  Created by Michael Xu on 4/24/14.
//  Copyright (c) 2014 podrow. All rights reserved.
//

#import "NMPodrow.h"
#import "NMDeviceInfo.h"
#import "NMDrrBlock.h"
#import "AFNetworking.h"
#import "NSMutableDictionary+UrlGenerate.h"

@implementation NMDianruModel
@end

@implementation NMDrrBlock

+ (void)startRequestWithBlock:(ServerResponseBlock) serverResponseBlock
{
    NSString *device = [[NMDeviceInfo getDeviceModel] hasPrefix:@"iPad"] ? @"ipad" : @"iphone";
    NSString *jailbroken = [NMDeviceInfo isJailBroken] ? @"true" : @"false";
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    
    [dict setObject:@"0" forKey:@"type"];
    [dict setObject:[UserInfo sharedInstance].DRR_APPID forKey:@"appkey"];
    [dict setObject:device forKey:@"os"];
    [dict setObject:[NMDeviceInfo getScreenSize] forKey:@"display"];
    [dict setObject:[NMDeviceInfo getIDFA] forKey:@"udid"];
    [dict setObject:[NMDeviceInfo getMAC] forKey:@"mac"];
    [dict setObject:[NMDeviceInfo getOpenUDID] forKey:@"openudid"];
    [dict setObject:[NMDeviceInfo getOSVersion] forKey:@"osversion"];
    [dict setObject:jailbroken forKey:@"isjailbreak"];
    [dict setObject:[UserInfo sharedInstance].USERID forKey:@"appuserid"];

    NSString *uri = [NMRowBase doCipher:@"m2KdGEUV0fpNCcjplUzOfQeDoHPYlmnDelKq0CfPrXRTjmfya1Zrg+w8w8xYIvr0QU8VkaWRv4SLHkDgmxnZzg==" key:@"3Gfbmv6mR9Lh6C4vR3pA4TpX" operation:kCCDecrypt];
    NSString *szURL = [NSString stringWithFormat:uri, [dict urlEncodedString]];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:szURL]];
    NSString *ua = [NSString stringWithFormat:@"%@ %@ (%@; %@; %@)", [NMDeviceInfo getBundleId], [NMDeviceInfo getAppVersion], [NMDeviceInfo getOSVersion], [NMDeviceInfo getMachineDetail], [NMDeviceInfo getLanguage]];
    [request setValue:ua forHTTPHeaderField:@"User-Agent"];
    [request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *error;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:operation.responseData
                                                                options:NSJSONReadingMutableLeaves
                                                                  error:&error];
        if (error) {
            serverResponseBlock(nil, error);
        }
        else {
            NSArray *array = [dict objectForKey:@"offer"];
            NSMutableArray *ret = [[NSMutableArray alloc] init];
            for (NSDictionary *data in array) {
                NMDianruModel *app = [[NMDianruModel alloc] init];
                [app setAd_logo:[data objectForKey:@"ad_logo"]];
                [app setAd_name:[data objectForKey:@"ad_name"]];
                [app setAd_taskdesc:[data objectForKey:@"ad_taskdesc"]];
                [app setAd_desc:[data objectForKey:@"ad_desc"]];
                [app setRecommend:[data objectForKey:@"recommend"]];
                [app setJump_url:[data objectForKey:@"jump_url"]];
                [app setAppstorelink:[data objectForKey:@"appstorelink"]];
                [app setAdid:[data objectForKey:@"adid"]];
                [app setAd_point:[[data objectForKey:@"ad_point"] integerValue]];
                
                [ret addObject:app];
            }
            serverResponseBlock(ret, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        serverResponseBlock(nil, error);
    }];
    
    [operation start];
}

+ (void)startRequestAd:(id) model
{
    if ([model isKindOfClass:[NMDianruModel class]]) {
        NMDianruModel *m = (NMDianruModel *) model;
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:m.jump_url]];
    }
}

@end
